package com.tracker

import java.util.Date;
import java.util.List;
import com.commonProject.common.exception.ValidateException
import com.commonProject.common.classes.HotelDTO;
import com.commonProject.common.intefaces.IHotel


class HotelService{
IHotel hotelServiceProxy
   static transactional = true     
   
  def getlist(params) {	  
	   println("HotelService Test")
	 //  println(params)
	   Long hotel_id=Long.valueOf(params.get("id"))
	   List<HotelDTO>el=null
	    el=hotelServiceProxy.getlist(hotel_id)
	   
	   return el.collect{[
		   text:it.title,
		   id:it.id
		   ]}	 	   
   }
  
   
   def getelement(params){
	   Long hotel_id=Long.valueOf(params.get("id"))	 
	   println("hotel_id ="+hotel_id)
	   HotelDTO el=null
	   el=hotelServiceProxy.getelement(hotel_id)
	   return[
		   'success':'true',
		   id:el.id,
		   title:el.title,
		   adress:el.adress,
		   website:el.website,
		   description:el.description,
		   dateCreated:el.dateCreated		   
		   ]
   }
		
	
	
	def deleteElement(params){
		println(params)
		Long hotel_id=Long.valueOf(params.get("id"))
		HotelDTO el = null
		
		try{
			el=hotelServiceProxy.deleteElement(hotel_id)	
		}catch(Exception ex){
			//println "-->EXCEPTION: ${ex}"
			println "No server connection"
			return ['success':'false']
		}
		return[success:true]	
	}
	
	def editElement(params){
		println("Hotel EDIT")
		Long hotel_id=Long.valueOf(params.get("id"))
		String h_title=params.get('editHotelTitle')
		String h_adress=params.get('editHotelAdress')
		String h_website=params.get('editHotelWebsite')
		String h_description=params.get('editHotelDescription')
		String h_dateCreated=params.get('editHotelDateCreated')
		
		try{
			def el=hotelServiceProxy.editElement(hotel_id,h_title,h_adress,h_website,h_description,h_dateCreated)
		}catch(ValidateException err){
		println(err.getError())
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
	}
	
	/*
	def editElement(params){
		println("Hotel EDIT")
		def edit = Hotel.get(params.get('id'))
		edit.title = params.get('editHotelTitle')
		edit.adress=params.get('editHotelAdress')
		edit.website=params.get('editHotelWebsite')
		edit.description=params.get('editHotelDescription')
		
		if (params.get('editHotelDateCreated')){
			edit.dateCreated = new Date(params.get('editHotelDateCreated'))
		}else{
			edit.dateCreated = null
		}
		if(edit.save()){
			return['success':'true']
		}else{
			def err = edit.errors.allErrors.collect{[
				errorMessage:messageSource.getMessage(it, null)
			]}
			println(err)
			return [
				'success':'false',
				errors:err				
				]
		}
	}*/
	
	def addElement(params) {
		println("ADD TUR")
		println(params)	
				
		Long hotel_id=Long.valueOf(params.get('id'))
		println("hotel_id ---"+hotel_id)
		String hotel_title=params.get('title')
		String hotel_adress=params.get('adress')
		String hotel_website=params.get('website')
		String hotel_description=params.get('description')
		
		try{
			def el=hotelServiceProxy.addElement(hotel_id,hotel_title,hotel_adress,hotel_website,hotel_description)
		}catch(ValidateException err){
		println(err.getError())
			return [
				'success' :'false',
				errors :err.getError()	
				]
		}
		return ['success':'true']
	}



}